package com.yalemang.androidclassroom.day03;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.lang.ref.WeakReference;

public class TestActivity extends AppCompatActivity {

    private int i =1;
    private MyHandler myHandler = new MyHandler(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    static class MyHandler extends Handler {

        //使用弱引用方式
        private WeakReference<TestActivity> weakReference;

        public MyHandler(TestActivity activity){
            this.weakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            weakReference.get().i = 3;
        }
    }
}
